﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Application.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    12/03/2012 20:12 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System.Collections;
using System.Configuration;
using System.Globalization;

#endregion

namespace IveelySE.Config
{
    public class Application
    {
        private static readonly object sync = new object();

        ///<summary>
        ///  配置文件控制类
        ///</summary>
        private static readonly Configuration Configuration =
            ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                                                                {
                                                                    ExeConfigFilename = "Iveely.config"
                                                                }, ConfigurationUserLevel.None);


        /// <summary>
        ///   获取设定的爬虫抓取首页
        /// </summary>
        /// <returns> </returns>
        public static ArrayList GetCrawlerUrl()
        {
            return new ArrayList(Configuration.AppSettings.Settings["Crawler"].Value.Split(','));
        }

        /// <summary>
        ///   获取关键字高亮的颜色
        /// </summary>
        /// <returns> </returns>
        public static string GetHighLight()
        {
            return Configuration.AppSettings.Settings["HighLight"].Value;
        }

        /// <summary>
        ///   获取隐马尔可夫模型的分词练习文件
        /// </summary>
        /// <returns> </returns>
        public static string GetTrainFile()
        {
            return Configuration.AppSettings.Settings["TrainFile"].Value;
        }

        /// <summary>
        ///   获取分词的分隔符
        /// </summary>
        /// <returns> </returns>
        public static string GetDelimiter()
        {
            return Configuration.AppSettings.Settings["Delimiter"].Value;
        }

        /// <summary>
        ///   爬虫爬行数据的临时存放目录
        /// </summary>
        /// <returns> </returns>
        public static string GetCrawlerTempDir()
        {
            return Configuration.AppSettings.Settings["CrawlerTemp"].Value;
        }

        /// <summary>
        ///   正式的数据存放目录
        /// </summary>
        /// <returns> </returns>
        public static string GetDataDir()
        {
            return Configuration.AppSettings.Settings["DataDir"].Value;
        }

        /// <summary>
        ///   获取PageRank计算文件结果路径
        /// </summary>
        /// <returns> </returns>
        public static string GetPagerankFile()
        {
            return Configuration.AppSettings.Settings["PageRankFile"].Value;
        }

        /// <summary>
        ///   获取PageRank的内容文件路径
        /// </summary>
        /// <returns> </returns>
        public static string GetPageRankContent()
        {
            return Configuration.AppSettings.Settings["PageRankContent"].Value;
        }

        /// <summary>
        ///   获取PageRank记录集
        /// </summary>
        /// <returns> </returns>
        public static string GetPageRankList()
        {
            return Configuration.AppSettings.Settings["PageRankList"].Value;
        }

        /// <summary>
        ///   获取缓存临时存放目录
        /// </summary>
        /// <returns> </returns>
        public static string GetIndexTemp()
        {
            return Configuration.AppSettings.Settings["IndexTemp"].Value;
        }

        /// <summary>
        ///   获取索引文件路径
        /// </summary>
        /// <returns> </returns>
        public static string GetIndexDir()
        {
            return Configuration.AppSettings.Settings["IndexDir"].Value;
        }

        /// <summary>
        ///   获取系统更新状态
        /// </summary>
        public static bool GetSystemUpdate()
        {
            return !Configuration.AppSettings.Settings["SystemUpdate"].Value.ToLower().Equals("false");
        }

        /// <summary>
        ///   获取当前处理页面的编号
        /// </summary>
        /// <returns> </returns>
        public static int GetCurrentPageIndex()
        {
            return int.Parse(Configuration.AppSettings.Settings["CurrentPageIndex"].Value);
        }

        /// <summary>
        ///   获取文件查找索引文件的文件名
        /// </summary>
        /// <returns> 返回文件名 </returns>
        public static string GetPageFindIndex()
        {
            return Configuration.AppSettings.Settings["PageFindIndex"].Value;
        }

        /// <summary>
        ///   获取知识文件的文件名
        /// </summary>
        /// <returns>返回知识文件名</returns>
        public static string GetEventFile()
        {
            return Configuration.AppSettings.Settings["Event"].Value;
        }

        /// <summary>
        ///  获取知识索引文件的文件名
        /// </summary>
        /// <returns>返回知识索引文件名</returns>
        public static string GetEventIndex()
        {
            return Configuration.AppSettings.Settings["EventIndex"].Value;
        }

        /// <summary>
        ///   获取是否启用的知识分析
        /// </summary>
        /// <returns>返回是否启用知识分析</returns>
        public static bool GetIsEnableEvent()
        {
            return Configuration.AppSettings.Settings["EnableEvent"].Value.Equals("1") ? true : false;
        }

        /// <summary>
        /// 获取返回结果数的个数
        /// </summary>
        /// <returns>返回个数</returns>
        public static int GetResultCount()
        {
            return int.Parse(Configuration.AppSettings.Settings["ResultCount"].Value);
        }

        /// <summary>
        ///   设置系统更新状态
        /// </summary>
        /// <param name="update"> 更新状态 </param>
        public static void SetSystemUpdate(bool update)
        {
            Save("SystemUpdate", update.ToString());
        }

        /// <summary>
        ///   设定当前处理网页编号累加
        /// </summary>
        public static void SetCurrentPageIndex()
        {
            Save("CurrentPageIndex", (GetCurrentPageIndex() + 1).ToString(CultureInfo.InvariantCulture));
        }

        private static void Save(string name, string value)
        {
            lock (sync)
            {
                Configuration.AppSettings.Settings[name].Value = value;
                Configuration.Save();
            }
        }
    }
}